package com.yunfei.common.auto.cratePage;


import com.yunfei.common.auto.GeneratorBase;
import com.yunfei.common.auto.util.CapitalizeUtil;
import com.yunfei.utils.string.StringUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
/**
 * 自动初始化页面
 * @author  贤云
 *
 */
public class GeneratorInitPage extends GeneratorBase {
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(GeneratorBase.DRIVER);
            String url = GeneratorBase.URL;
            String user = GeneratorBase.NAME;
            String pass = GeneratorBase.PASS;
            conn = DriverManager.getConnection(url,user,pass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void main(String[] args){
        GeneratorInitPage generatorInitPageDB=new GeneratorInitPage();
        generatorInitPageDB.generatorInitPage();
    }
    public  void generatorInitPage(){
        Connection conn=getConnection();
        String sql="select * from "+tablename;
        PreparedStatement stmt;
        String content="";
        String queryParam="";//查询参数
        try {
            stmt = conn.prepareStatement(sql);
            ResultSet rs=stmt.executeQuery(sql);
            ResultSetMetaData data=rs.getMetaData();
            DatabaseMetaData dbmd=conn.getMetaData();
            while(rs.next()){
                for(int i = 1 ; i<= data.getColumnCount() ; i++){
                    //获得所有列的数目及实际列数
                    int columnCount=data.getColumnCount();
                    //获得指定列的列名
                    String columnName = data.getColumnName(i);
                    String columnRemark="";
                    String columnRemarkAll="";
                    ResultSet result = dbmd.getColumns(null, "%", tablename, "%");
                    while(result.next()){
                        if(columnName.contains(result.getString("COLUMN_NAME"))){
                            columnRemark= CapitalizeUtil.initcap(result.getString("REMARKS"));
                            columnRemarkAll=columnRemark;
                            //以分号分隔，可以添加额外的信息
                            columnRemark=columnRemark.split(";")[0];
                        }
                    }
                    //获得指定列的列值
                    String columnValue = rs.getString(i);
                    //获得指定列的数据类型
                    int columnType=data.getColumnType(i);
                    //获得指定列的数据类型名
                    String columnTypeName=data.getColumnTypeName(i);
                    //所在的Catalog名字
                    String catalogName=data.getCatalogName(i);
                    //对应数据类型的类
                    String columnClassName=data.getColumnClassName(i);
                    //在数据库中类型的最大字符个数
                    int columnDisplaySize=data.getColumnDisplaySize(i);
                    //默认的列的标题
                    String columnLabel=CapitalizeUtil.initcap(data.getColumnLabel(i));
                    //获得列的模式
                    String schemaName=data.getSchemaName(i);
                    //某列类型的精确度(类型的长度)
                    int precision= data.getPrecision(i);
                    //小数点后的位数
                    int scale=data.getScale(i);
                    //获取某列对应的表名
                    String tableName=data.getTableName(i);
                    // 是否自动递增
                    boolean isAutoInctement=data.isAutoIncrement(i);
                    //在数据库中是否为货币型
                    boolean isCurrency=data.isCurrency(i);
                    //是否为空
                    int isNullable=data.isNullable(i);
                    //是否为只读
                    boolean isReadOnly=data.isReadOnly(i);
                    //能否出现在where中
                    boolean isSearchable=data.isSearchable(i);
                    // System.out.println(columnCount);
                  /*  System.out.println("获得列"+i+"的字段名称:"+columnName);
                    //  System.out.println("获得列"+i+"的字段值:"+columnValue);
                    // System.out.println("获得列"+i+"的类型,返回SqlType中的编号:"+columnType);
                    System.out.println("获得列"+i+"的数据类型名:"+columnTypeName);
                    // System.out.println("获得列"+i+"所在的Catalog名字:"+catalogName);
                    System.out.println("获得列"+i+"对应数据类型的类:"+columnClassName);
                    System.out.println("获得列"+i+"在数据库中类型的最大字符个数:"+columnDisplaySize);
                    //System.out.println("获得列"+i+"的默认的列的标题:"+columnLabel);
                    // System.out.println("获得列"+i+"的模式:"+schemaName);
                    System.out.println("获得列"+i+"类型的精确度(类型的长度):"+precision);
                    System.out.println("获得列"+i+"小数点后的位数:"+scale);
                    System.out.println("获得列"+i+"对应的表名:" + tableName);
                    System.out.println("获得列"+i+"是否自动递增:"+isAutoInctement);
                    System.out.println("获得列"+i+"在数据库中是否为货币型:"+isCurrency);
                    System.out.println("获得列"+i+"是否为空:"+isNullable);
                    System.out.println("获得列"+i+"是否为只读:"+isReadOnly);*/
                    // System.out.println("获得列"+i+"能否出现在where中:"+isSearchable);
                    //判断是否需要添加查询语句
                    if(!StringUtil.isNullOrEmpty(columnRemarkAll)){
                        if(columnRemarkAll.contains("query")){
                            queryParam+="" +
                                    "\t\t\t\t\t\t\t\t"+columnRemark+"：<input type=\"text\" id=\""+columnLabel+"\" name=\""+columnLabel+"\">\n" ;
                        }
                    }
                }
            }
            /****************************************自动生成init.jsp页面 start******************************************************************************/

            try {
                content=getInitPageContent(pageTitle, pageOutPath,queryParam);
                File directory = new File("");
                //String outputPath = directory.getAbsolutePath()+ "/web/"+ pageOutPath+"/init.jsp";
                String outputPath = /*directory.getAbsolutePath()+ "/web/"+ */pageOutPath+"/init.jsp";
                File file=new File(outputPath);
                file.getParentFile().mkdirs();
                FileWriter fw = new FileWriter(outputPath);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(content);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            /****************************************自动生成init.jsp页面 end******************************************************************************/
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
        }
    }

    /**
     *生成初始化页面
     * @param title 页面标题
     * @param path s9/v3/pastInvestmentCase 页面所在路径
     * @return
     */
    public static  String getInitPageContent(String title,String path,String queryParam){
        String content="";
        content="<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" +
                "<%@ page language=\"java\" contentType=\"text/html;charset=utf-8\" pageEncoding=\"utf-8\"%>\n" +
                "<%@taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\" %>\n" +
                "<%\n" +
                "String path = request.getContextPath();\n" +
                "String basePath = request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\";\n" +
                "%>\n" +
                "<html>\n" +
                "\t<head>\n" +
                "\t\t<title>"+title+"</title>\n" +
                "\t\t<!-- 引用本页面JS、CSS样式静态资源 -->\n" +
                "\t\t<%@include file=\"/s9/common/common.jsp\"%>\n" +
                "\t\t<!-- 用户管理操作JS静态资源的引用 -->\n" +
                "\t\t<link  rel=\"stylesheet\" type=\"text/css\"  href=\"<%=basePath%>/s9/res/js/layui/css/global.css\"/>\n" +
                "\t\t<link  rel=\"stylesheet\" type=\"text/css\"  href=\"<%=basePath%>/s9/res/js/layui/css/layui.css\"/>\n" +
                "\t\t<script type=\"text/javascript\" src=\"<%=basePath %>/"+path+"/js/form.js?v=${version}\"></script>\n" +
                "\t\t<script src=\"<%=basePath%>/s9/res/js/layui/layui.js\" type=\"text/javascript\"></script>\n" +
                "\t</head>\n" +
                "\t<script type=\"text/javascript\">\n" +
                "\t$(function (){\n" +
                "\t \t\tvar flag=\"${backInfo}\";\n" +
                "\t\t\tif(flag=='1'){\n" +
                "\t\t\t\twindow.parent.reload(\"保存成功\");\n" +
                "\t\t\t}else if(flag=='0'){\n" +
                "\t\t\t\t$.ligerDialog.error(\"保存失败\");\n" +
                "\t\t\t\treturn;\n" +
                "\t\t\t}\n" +
                "\t\t});\n" +
                "\t</script>\n" +
                "\t<body class=\"z-body-detail\">\n" +
                "\t<form id=\"queryForm\" method=\"post\">\n"+
                "\t\t<table width=\"100%\" id=\"js_layoutTable\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" height=\"*\" class=\"js_layoutTable\">\n" +
                "\t\t\t<tbody>\n" +
                "\t\t\t\t<tr>\n" +
                "\t\t\t\t\t<td>\n" +
                "\t\t\t\t\t\t<div class=\"z-toolbar\" id=\"ToolBar1\">\n" +
                "\t\t\t\t\t\t\t<div class=\"z-toolbar-ct\">\n" +
                "\t\t\t\t\t\t\t\t<div class=\"z-toolbar-overflow\"> \n" +
                "\t\t\t\t\t\t\t\t\t<div class=\"z-toolbar-nowrap\">\n" +
                "\t\t\t\t\t\t\t\t\t\t<a href=\"javascript:void(0);\" id=\"Button2\"\tclass=\"z-btn z-btn-flat\" onclick=\"addInitPage();\">\n" +
                "\t\t\t\t\t\t\t\t\t\t\t<img class=\"icon021a2\" src=\"../s9/res/img/icon000.png\"><b>添加<i></i></b>\n" +
                "\t\t\t\t\t\t\t\t\t\t</a>\n" +
                "\t\t\t\t\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t\t</div>\n" +
                "\n" +
                "\t\t\t\t\t</td>\n" +
                "\t\t\t\t</tr>\n" ;
                content+="\t\t\t\t<tr>\n" +
                        "\t\t\t\t\t<td>\n" +
                        "\t\t\t\t\t\t<div class=\"gradient\" style=\"padding: 5px 12px;\">\n" +
                        "\t\t\t\t\t\t\t<div style=\"float: left; white-space: nowrap;padding-left: 20px;\">\n" ;
                content+=queryParam;
                 content+="\t\t\t\t\t\t\t\t<input type=\"button\" name=\"Submitbutton\" id=\"Submitbutton\" value=\"搜索\" onclick=\"doSearch();\">\n" +
                        "\t\t\t\t\t\t\t</div>\n" +
                        "\t\t\t\t\t\t</div>\n" +
                        "\t\t\t\t\t</td>\n" +
                        "\t\t\t\t</tr>\n";
                content+="\t\t\t\t<tr valign=\"top\">\n" +
                "\t\t\t\t\t<!-- 数据信息 --> \n" +
                "\t\t\t\t\t<td style=\"padding: 5px; height: auto;\">\n" +
                "\t\t\t\t\t\t<div class=\"l-loading\" style=\"display:block\" id=\"pageloading\"></div> \n" +
                "\t\t\t\t\t\t<div class=\"l-clear\"></div>\n" +
                "\t\t\t\t\t\t<div id=\"maingrid\"></div>\n" +
                "\t\t\t\t\t</td> \n" +
                "\t\t\t\t</tr>\n" +
                "\t\t\t</tbody>\n" +
                "\t\t</table>\n" +
                "\t\t</form>\n"+
                "\t</body>\n" +
                "</html>\n" +
                "\n";
        return content;
    }
}
